package com.asset.dispoal.business.repayment.service.strategy;

import com.asset.dispoal.business.bsAgreement.domain.db.entity.AgreementInfo;
import com.asset.dispoal.business.bsAgreement.domain.db.mapper.AgreementMapper;
import com.asset.dispoal.business.bsCase.domain.db.entity.BsCase;
import com.asset.dispoal.business.repayment.domain.db.entity.BsCaseRepayment;
import com.asset.dispoal.business.repayment.domain.db.mapper.CaseRepaymentMapper;
import com.asset.dispoal.business.repayment.dto.RepaymentInfoResponse;
import com.asset.dispoal.commom.menus.AgreementCaseSateMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * @description: 立案
 * @author: yf
 * @date: 2022-09-28 14:00:31
 */
@Component("registerCaseState")
public class RegisterCase implements IRepaymentStrategy {
    @Autowired
    private AgreementMapper agreementMapper;
    @Autowired
    private CaseRepaymentMapper caseRepaymentMapper;

    @Override
    public RepaymentInfoResponse getRepaymentInfo(BsCase bsCase) {
        AgreementCaseSateMenu caseSateMenu = AgreementCaseSateMenu.getByLabel(bsCase.getCaseState());
        //还款信息是否存在
        BsCaseRepayment caseRepayment = caseRepaymentMapper.queryByCaseId(bsCase.getId(), bsCase.getCaseState());
        if (null != caseRepayment) {
            return RepaymentInfoResponse.builder()
                    .repaymentId(caseRepayment.getId())
                    .caseId(bsCase.getId())
                    .caseState(bsCase.getCaseState())
                    .caseStateName(caseSateMenu.getValue())
                    .lawsuitPrincipalAmount(nullValue(caseRepayment.getPrincipalAmount()))
                    .lawsuitInterestAmount(nullValue(caseRepayment.getInterestAmount()))
                    .litigationFines(nullValue(caseRepayment.getLitigationFines()))
                    .litigationAmount(nullValue(caseRepayment.getLitigationAmount()))
                    .preservationAmount(nullValue(caseRepayment.getPreservationAmount()))
                    .toDoReturnPrincipalAmount(nullValue(bsCase.getPrincipal()))
                    .toDoInterestAmount(nullValue(bsCase.getInterest()))
                    .toDoLitigationFines(nullValue(bsCase.getPenaltyInterest()))
                    .toDoLitigationAmount(nullValue(bsCase.getCaseAcceptAmount()))
                    .todoPreservationAmount(nullValue(bsCase.getJudgmentPreservationAmount()))
                    .repaymentPrincipal(nullValue(caseRepayment.getRepaymentPrincipal()))
                    .repaymentInterest(nullValue(caseRepayment.getRepaymentInterest()))
                    .repaymentPenalty(nullValue(caseRepayment.getRepaymentPenalty()))
                    .repaymentLitigationAmount(nullValue(caseRepayment.getRepaymentLitigationAmount()))
                    .repaymentPreservationAmount(nullValue(caseRepayment.getRepaymentPreservationAmount()))
                    .build();
        }

        ///合同集合，未处理逾期时间与总额
        List<AgreementInfo> agreementInfos = agreementMapper.queryAgreementInfoByCaseId(bsCase.getId());
        //应还本金
        String principalRepayment = agreementInfos.stream()
                .map(AgreementInfo::getLawsuitPrincipalAmount)
                .map(BigDecimal::new)
                .reduce(BigDecimal.ZERO, BigDecimal::add).toString();
        //应还利息
        String interestRepayment = agreementInfos.stream()
                .map(AgreementInfo::getLawsuitInterestAmount)
                .map(BigDecimal::new)
                .reduce(BigDecimal.ZERO, BigDecimal::add).toString();


        RepaymentInfoResponse response = RepaymentInfoResponse.builder()
                .caseId(bsCase.getId())
                .caseState(bsCase.getCaseState())
                .caseStateName(caseSateMenu.getValue())
                .lawsuitPrincipalAmount(bsCase.getLawsuitPrincipalAmount().toPlainString())
                .lawsuitInterestAmount(bsCase.getLawsuitInterestAmount().toPlainString())
                .litigationAmount(bsCase.getLitigationAmount().toPlainString())
                .preservationAmount(bsCase.getPreservationAmount().toString())
                .toDoReturnPrincipalAmount(bsCase.getLawsuitPrincipalAmount().toPlainString())
                .toDoInterestAmount(bsCase.getLawsuitInterestAmount().toPlainString())
                .toDoLitigationAmount(bsCase.getLitigationAmount().toPlainString())
                .todoPreservationAmount(bsCase.getPreservationAmount().toString())
                .repaymentPrincipal(BigDecimal.ZERO.toPlainString())
                .repaymentInterest(BigDecimal.ZERO.toPlainString())
                .repaymentLitigationAmount(BigDecimal.ZERO.toPlainString())
                .repaymentPreservationAmount(BigDecimal.ZERO.toPlainString())
                .litigationAmount(bsCase.getLitigationAmount().toPlainString())
                .preservationAmount(bsCase.getPreservationAmount().toPlainString())
                .build();
        return response;
    }

    private String nullValue(BigDecimal bigDecimal) {
        return bigDecimal == null ? BigDecimal.ZERO.toPlainString() : bigDecimal.toPlainString();
    }
}
